package com.hzjiteng.newsbymvp.news.model;

import com.hzjiteng.newsbymvp.bean.NewsBean;
import com.hzjiteng.newsbymvp.bean.NewsDetailBean;
import com.hzjiteng.newsbymvp.comment.OnFailure;
import com.hzjiteng.newsbymvp.comment.URLS;
import com.hzjiteng.newsbymvp.news.NewsJsonUtils;
import com.hzjiteng.newsbymvp.news.widget.NewsFragment;
import com.hzjiteng.newsbymvp.utils.OkHttpUtils;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @Email:lijiayan_mail@163.com
 * @created_time 2016/07/29 13:57
 */
public class NewsModelImpl implements NewsModel{

    /**
     * 加载
     * @param url
     * @param type
     * @param onLoadNewsListener
     */
    @Override
    public void loadNews(String url, final int type, final OnLoadNewsListener onLoadNewsListener) {
        OkHttpUtils.ResultCallBack<String> loadNewsCallBack = new OkHttpUtils.ResultCallBack<String>() {
            @Override
            public void onSuccess(String respose) {
                List<NewsBean>newsBeanList = NewsJsonUtils.readJsonNewsBeans(respose,getID(type));
                onLoadNewsListener.onSuccess(newsBeanList);
            }

            @Override
            public void onFailure(Exception e) {
                onLoadNewsListener.onFailure(e,"加载新闻列表失败");
            }
        };

        OkHttpUtils.doGet(url,loadNewsCallBack);
    }

    @Override
    public void loadNewsDetail(final String docId, final OnLoadNewsDetailListener onLoadNewsDetailListener) {
        String url = getDetailUrl(docId);
        OkHttpUtils.ResultCallBack<String>loadNewsDetailCallBack = new OkHttpUtils.ResultCallBack<String>() {
            @Override
            public void onSuccess(String respose) {
                NewsDetailBean detailBean = NewsJsonUtils.readJsonNewsDetailBeans(respose,docId);
                onLoadNewsDetailListener.onSuccess(detailBean);
            }

            @Override
            public void onFailure(Exception e) {
                onLoadNewsDetailListener.onFailure(e,"加载新闻详情失败");
            }
        };
        OkHttpUtils.doGet(url,loadNewsDetailCallBack);
    }

    public interface OnLoadNewsListener extends OnFailure {
        void onSuccess(List<NewsBean> list);
    }

    public interface OnLoadNewsDetailListener extends OnFailure {
        void onSuccess(NewsDetailBean detailBean);
    }


    /**
     * 获取ID
     * @param type
     * @return
     */
    private String getID(int type) {
        String id;
        switch (type) {
            case NewsFragment.NEWS_TYPE_TOP:
                id = URLS.TOP_ID;
                break;
            case NewsFragment.NEWS_TYPE_NBA:
                id = URLS.NBA_ID;
                break;
            case NewsFragment.NEWS_TYPE_CARS:
                id = URLS.CAR_ID;
                break;
            case NewsFragment.NEWS_TYPE_JOKES:
                id = URLS.JOKE_ID;
                break;
            default:
                id = URLS.TOP_ID;
                break;
        }
        return id;
    }


    private String getDetailUrl(String docId) {
        StringBuffer sb = new StringBuffer(URLS.NEW_DETAIL);
        sb.append(docId).append(URLS.END_DETAIL_URL);
        return sb.toString();
    }


}
